package com.common.account.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.account.entity.AuPermission;
import com.common.account.mapper.AuPermissionMapper;
import com.common.account.service.IAuPermissionService;
import com.common.basis.oauth.AuUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 *
 * @author wangguochao
 * @since 2020-01-06
 */
@Service
@Slf4j
public class AuPermissionServiceImpl extends ServiceImpl<AuPermissionMapper, AuPermission> implements IAuPermissionService {
    @Resource
    private AuPermissionMapper auPermissionMapper;


    /**
     * 发现树
     *
     * @param userName 用户名
     * @return {@link List}<{@link AuPermission}>
     */
    @Override
    public List<AuPermission> findTree(String userName) {
        List<AuPermission> list = auPermissionMapper.findTreeByUserName(userName);
        return recursion(list, "top");
    }

    /**
     * 找到树的用户名
     *
     * @param userName 用户名
     * @param scope    范围
     * @param type     类型
     * @return {@link List}<{@link AuPermission}>
     */
    @Override
    public List<AuPermission> findTreeByUserName(String userName, String scope, String type) {
        List<AuPermission> list = auPermissionMapper.findTreeByUserName(userName);
//        list = list.stream().sorted(Comparator.comparing(AuPermission::getSortNumber)
//                        .thenComparing(AuPermission::getSortTime)
//                        .reversed())
//                .collect(Collectors.toList());
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (scope.equals(auPermission.getPermissionCode())) {
                List<AuPermission> recursion;
                if (type == null) {
                    recursion = this.recursion(list, auPermission.getId());
                } else {
                    recursion = this.recursion(list, auPermission.getId(), type);
                }
                auPermission.setChildren(recursion);
                arr.add(auPermission);
            }
        }
        return arr;
    }


    /**
     * 找到的用户名
     *
     * @param userName 用户名
     * @return {@link List}<{@link AuPermission}>
     */
    @Override
    public List<AuPermission> findByUserName(String userName) {
        return auPermissionMapper.findTreeByUserName(userName);
    }

    /**
     * 找到的用户id
     *
     * @param userId 用户id
     * @return {@link List}<{@link String}>
     */
    @Override
    public List<String> findByUserId(String userId) {
        return auPermissionMapper.findByUserId(userId);
    }

    /**
     * 找到用户的权限
     *
     * @param permission 许可
     * @return {@link List}<{@link AuUser}>
     */
    @Override
    public List<AuUser> findUserByPermission(String permission) {
        return auPermissionMapper.findUserByPermission(permission);
    }

    /**
     * 找到的用户名和类型
     *
     * @param name 的名字
     * @param type 类型
     * @return {@link List}<{@link String}>
     */
    @Override
    public List<String> findByUserNameAndType(String name, String type) {
        return auPermissionMapper.findByUserNameAndType(name, type);
    }

    /**
     * 获取最大排序数量根据父id
     *
     * @param parentId 父id
     * @return {@code Integer}
     */
    @Override
    public Integer getMaxSortNumberByParentId(String parentId) {
        return auPermissionMapper.getMaxSortNumberByParentId(parentId);
    }


    /**
     * 递归
     *
     * @param list     列表
     * @param parentId 父id
     * @return {@link List}<{@link AuPermission}>
     */
    private List<AuPermission> recursion(List<AuPermission> list, String parentId) {
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (parentId.equals(auPermission.getParentId())) {
                List<AuPermission> r = recursion(list, auPermission.getId());
                auPermission.setChildren(r);
                arr.add(auPermission);
            }
        }
        return arr;
    }

    /**
     * 递归
     *
     * @param list     列表
     * @param parentId 父id
     * @param type     类型
     * @return {@link List}<{@link AuPermission}>
     */
    private List<AuPermission> recursion(List<AuPermission> list, String parentId, String type) {
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (type.equals(auPermission.getPermissionGroups()) && parentId != null && parentId.equals(auPermission.getParentId())) {
                List<AuPermission> r = recursion(list, auPermission.getId(), type);
                auPermission.setChildren(r);
                arr.add(auPermission);
            }
        }
        return arr;
    }

}
